﻿using System;
using System.Collections.Specialized;
using System.Text;
using System.Web;

namespace wojilu
{
    /// <summary>
    /// Cookie操作类
    /// </summary>
    public static class CookieHelper
    {

        public static String encoding = "utf-8";

        /// <summary>
        /// 创建COOKIE对象并赋Value值，修改COOKIE的Value值也用此方法，因为对COOKIE修改必须重新设Expires
        /// </summary>
        /// <param name="strCookieName">COOKIE对象名</param>
        /// <param name="strValue">COOKIE对象Value值</param>
        public static void SetObj(string strCookieName, string strValue)
        {
            SetObj(strCookieName, 1, strValue, "", "/");
        }


        /// <summary>
        /// 创建COOKIE对象并赋Value值，修改COOKIE的Value值也用此方法，因为对COOKIE修改必须重新设Expires
        /// </summary>
        /// <param name="strCookieName">COOKIE对象名</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)，</param>
        /// <param name="strValue">COOKIE对象Value值</param>
        public static void SetObj(string strCookieName, int iExpires, string strValue)
        {
            SetObj(strCookieName, iExpires, strValue, "", "/");
        }


        /// <summary>
        /// 创建COOKIE对象并赋Value值，修改COOKIE的Value值也用此方法，因为对COOKIE修改必须重新设Expires
        /// </summary>
        /// <param name="strCookieName">COOKIE对象名</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)，</param>
        /// <param name="strValue">COOKIE对象Value值</param>
        /// <param name="strDomains">作用域,多个域名用;隔开</param>
        public static void SetObj(string strCookieName, int iExpires, string strValue, string strDomains)
        {
            SetObj(strCookieName, iExpires, strValue, strDomains, "/");
        }


        /// <summary>
        /// 创建COOKIE对象并赋Value值，修改COOKIE的Value值也用此方法，因为对COOKIE修改必须重新设Expires
        /// </summary>
        /// <param name="strCookieName">COOKIE对象名</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)，</param>
        /// <param name="strValue">COOKIE对象Value值</param>
        /// <param name="strDomains">作用域,多个域名用;隔开</param>
        /// <param name="strPath">作用路径</param>
        public static void SetObj(string strCookieName, int iExpires, string strValue, string strDomains, string strPath)
        {
            string _strDomain = SelectDomain(strDomains);
            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());
            objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim());
            if (_strDomain.Length > 0)
                objCookie.Domain = _strDomain;
            if (iExpires > 0)
            {
                if (iExpires == 1)
                    objCookie.Expires = DateTime.MaxValue;
                else
                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }


        /// <summary>
        /// 创建COOKIE对象并赋多个KEY键值
        /// 设键/值如下：
        /// NameValueCollection myCol = new NameValueCollection();
        /// myCol.Add("red", "rojo");
        /// myCol.Add("green", "verde");
        /// myCol.Add("blue", "azul");
        /// myCol.Add("red", "rouge");   结果“red:rojo,rouge；green:verde；blue:azul”
        /// </summary>
        /// <param name="strCookieName">COOKIE对象名</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)，</param>
        /// <param name="KeyValue">键/值对集合</param>
        public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue)
        {
            SetObj(strCookieName, iExpires, KeyValue, "", "/");
        }


        public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomains)
        {
            SetObj(strCookieName, iExpires, KeyValue, strDomains, "/");
        }


        /// <summary>
        /// 创建COOKIE对象并赋多个KEY键值
        /// 设键/值如下：
        /// NameValueCollection myCol = new NameValueCollection();
        /// myCol.Add("red", "rojo");
        /// myCol.Add("green", "verde");
        /// myCol.Add("blue", "azul");
        /// myCol.Add("red", "rouge");   结果“red:rojo,rouge；green:verde；blue:azul”
        /// </summary>
        /// <param name="strCookieName">COOKIE对象名</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)，</param>
        /// <param name="KeyValue">键/值对集合</param>
        /// <param name="strDomains">作用域,多个域名用;隔开</param>
        /// <param name="strPath">作用路径</param>
        public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomains, string strPath)
        {
            string _strDomain = SelectDomain(strDomains);
            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());
            foreach (String key in KeyValue.AllKeys)
            {
                objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim());
            }
            if (_strDomain.Length > 0)
                objCookie.Domain = _strDomain;
            objCookie.Path = strPath.Trim();
            if (iExpires > 0)
            {
                if (iExpires == 1)
                    objCookie.Expires = DateTime.MaxValue;
                else
                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        /// <summary>
        /// 读取Cookie某个对象的Value值，返回Value值，如果对象本就不存在，则返回字符串null
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <returns>Value值，如果对象本就不存在，则返回字符串null</returns>
        public static string GetValue(string strCookieName)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
            {
                return null;
            }
            else
            {
                string _value = HttpContext.Current.Request.Cookies[strCookieName].Value;
                return HttpContext.Current.Server.UrlDecode(_value);
            }
        }
        /// <summary>
        /// 读取Cookie某个对象的某个Key键的键值，返回Key键值，如果对象本就不存在，则返回字符串null，如果Key键不存在，则返回字符串"KeyNonexistence"
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strKeyName">Key键名</param>
        /// <returns>Key键值，如果对象本就不存在，则返回字符串null，如果Key键不存在，则返回字符串"KeyNonexistence"</returns>
        public static string GetValue(string strCookieName, string strKeyName)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
            {
                return null;
            }
            else
            {
                string strObjValue = HttpContext.Current.Request.Cookies[strCookieName].Value;
                string strKeyName2 = strKeyName + "=";
                //if (strObjValue.IndexOf(strKeyName2) == -1)
                if (!strObjValue.Contains(strKeyName2))
                    return null;
                else
                {
                    string _value = HttpContext.Current.Request.Cookies[strCookieName][strKeyName];
                    return HttpContext.Current.Server.UrlDecode(_value);
                }
            }
        }
        /// <summary>
        /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法，操作成功返回字符串"success"，如果对象本就不存在，则返回字符串null。
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strKeyName">Key键名</param>
        /// <param name="KeyValue">Key键值</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)。注意：虽是修改功能，实则重建覆盖，所以时间也要重设，因为没办法获得旧的有效期</param>
        /// <returns>如果对象本就不存在，则返回字符串null，如果操作成功返回字符串"success"。</returns>
        public static string Edit(string strCookieName, string strKeyName, string KeyValue, int iExpires)
        {
            return Edit(strCookieName, strKeyName, KeyValue, iExpires, "", "/");
        }
        /// <summary>
        /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法，操作成功返回字符串"success"，如果对象本就不存在，则返回字符串null。
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strKeyName">Key键名</param>
        /// <param name="KeyValue">Key键值</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)。注意：虽是修改功能，实则重建覆盖，所以时间也要重设，因为没办法获得旧的有效期</param>
        /// <param name="strPath">作用路径</param>
        /// <returns>如果对象本就不存在，则返回字符串null，如果操作成功返回字符串"success"。</returns>
        public static string Edit(string strCookieName, string strKeyName, string KeyValue, int iExpires, string strPath)
        {
            return Edit(strCookieName, strKeyName, KeyValue, iExpires, "", strPath);
        }
        /// <summary>
        /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法，操作成功返回字符串"success"，如果对象本就不存在，则返回字符串null。
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strKeyName">Key键名</param>
        /// <param name="KeyValue">Key键值</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)。注意：虽是修改功能，实则重建覆盖，所以时间也要重设，因为没办法获得旧的有效期</param>
        /// <param name="strDomains">作用域,多个域名用;隔开</param>
        /// <param name="strPath">作用路径</param>
        /// <returns>如果对象本就不存在，则返回字符串null，如果操作成功返回字符串"success"。</returns>
        public static string Edit(string strCookieName, string strKeyName, string KeyValue, int iExpires, string strDomains, string strPath)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
                return null;
            else
            {
                HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName];
                objCookie[strKeyName] = HttpContext.Current.Server.UrlEncode(KeyValue.Trim());
                if (iExpires > 0)
                {
                    if (iExpires == 1)
                        objCookie.Expires = DateTime.MaxValue;
                    else
                        objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                }
                HttpContext.Current.Response.Cookies.Add(objCookie);
                return "success";
            }
        }
        /// <summary>
        /// 删除COOKIE对象
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        public static void Del(string strCookieName)
        {
            Del(strCookieName, "", "/");
        }
        /// <summary>
        /// 删除COOKIE对象
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strDomains">作用域,多个域名用;隔开</param>
        public static void Del(string strCookieName, string strDomains)
        {
            Del(strCookieName, strDomains, "/");
        }
        /// <summary>
        /// 删除COOKIE对象
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strDomains">作用域,多个域名用;隔开</param>
        /// <param name="strPath">作用路径</param>
        public static void Del(string strCookieName, string strDomains, string strPath)
        {
            string _strDomain = SelectDomain(strDomains);
            HttpCookie objCookie = new HttpCookie(strCookieName.Trim());
            if (_strDomain.Length > 0)
                objCookie.Domain = _strDomain;
            objCookie.Path = strPath.Trim();
            objCookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        /// <summary>
        /// 删除某个COOKIE对象某个Key子键，操作成功返回字符串"success"，如果对象本就不存在，则返回字符串null
        /// </summary>
        /// <param name="strCookieName">Cookie对象名称</param>
        /// <param name="strKeyName">Key键名</param>
        /// <param name="iExpires">COOKIE对象有效时间（秒数），1表示永久有效，0和负数都表示不设有效时间，大于等于2表示具体有效秒数，31536000秒=1年=(60*60*24*365)。注意：虽是修改功能，实则重建覆盖，所以时间也要重设，因为没办法获得旧的有效期</param>
        /// <returns>如果对象本就不存在，则返回字符串null，如果操作成功返回字符串"success"。</returns>
        public static string DelKey(string strCookieName, string strKeyName, int iExpires)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
            {
                return null;
            }
            else
            {
                HttpCookie objCookie = HttpContext.Current.Request.Cookies[strCookieName];
                objCookie.Values.Remove(strKeyName);
                if (iExpires > 0)
                {
                    if (iExpires == 1)
                        objCookie.Expires = DateTime.MaxValue;
                    else
                        objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                }
                HttpContext.Current.Response.Cookies.Add(objCookie);
                return "success";
            }
        }
        /// <summary>
        /// 定位到正确的域
        /// </summary>
        /// <param name="strDomains"></param>
        /// <returns></returns>
        private static string SelectDomain(string strDomains)
        {
            bool _isLocalServer = false;
            if (strDomains.Trim().Length == 0)
                return "";
            string _thisDomain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
            //if (_thisDomain.IndexOf(".") < 0)//说明是计算机名，而不是域名
            if (!_thisDomain.Contains("."))
                _isLocalServer = true;
            string _strDomain = "www.abc.com";//这个域名是瞎扯
            string[] _strDomains = strDomains.Split(';');
            for (int i = 0; i < _strDomains.Length; i++)
            {
                //if (_thisDomain.IndexOf(_strDomains[i].Trim()) < 0)//判断当前域名是否在作用域内
                if (!_thisDomain.Contains(_strDomains[i].Trim()))
                    continue;
                else
                {
                    //区分真实域名(或IP)与计算机名
                    if (_isLocalServer)
                        _strDomain = "";//作用域留空，否则Cookie不能写入
                    else
                        _strDomain = _strDomains[i].Trim();
                    break;
                }
            }
            return _strDomain;
        }

        #region CookieHelper

        #region 获取Cookie
        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <returns></returns>
        public static string GetCookie(string CookieName)
        {
            return GetCookie(CookieName, "", encoding);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="CookieName"></param>
        /// <param name="i">0为不需要前缀，1为需要前缀</param>
        /// <returns></returns>
        public static string GetCookie(string CookieName, int i)
        {
            if (i == 0) return GetCookie(CookieName, "", encoding);
            else return GetCookie(CookieName, "", encoding);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="CookieName">Cookie Name</param>
        /// <param name="key">子项</param>
        /// <returns></returns>
        public static string GetCookie(string CookieName, string key)
        {
            return GetCookie(CookieName, key, encoding);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="CookieName">Cookie Name</param>
        /// <param name="key">子项</param>
        /// <param name="i">0为不需要前缀，1为需要前缀</param>
        /// <returns></returns>
        public static string GetCookie(string CookieName, string key, int i)
        {
            if (i == 0) return GetCookie(CookieName, key, encoding);
            else return GetCookie(CookieName, key, encoding);
        }

        /// <summary>
        /// 根据HttpRequest获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="request">HttpRequest Object</param>
        /// <returns></returns>
        public static string GetCookie(string name, HttpRequest request)
        {
            if (request.Cookies.Get(name) != null)
                return HttpUtility.UrlDecode(request.Cookies[name].Value);
            else
                return null;
        }

        /// <summary>
        /// 根据HttpRequest获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="request">HttpRequest Object</param>
        /// <param name="i">0为不需要前缀，1为需要前缀</param>
        /// <returns></returns>
        public static string GetCookie(string name, HttpRequest request, int i)
        {
            if (i == 0)
            {
                if (request.Cookies.Get(name) != null)
                    return HttpUtility.UrlDecode(request.Cookies[name].Value);
                else
                    return null;
            }
            else
            {
                if (request.Cookies.Get(name) != null)
                    return HttpUtility.UrlDecode(request.Cookies[name].Value);
                else
                    return null;
            }
        }

        /// <summary>
        /// 根据HttpRequest获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="request">HttpRequest Object</param>
        /// <returns></returns>
        public static string GetCookie(string name, HttpRequest request, string encoding)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            if (request.Cookies.Get(name) != null)
                return HttpUtility.UrlDecode(request.Cookies[name].Value, stre);
            else
                return null;
        }

        /// <summary>
        /// 根据HttpRequest获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="request">HttpRequest Object</param>
        /// <returns></returns>
        public static string GetCookie(string name, HttpRequest request, string encoding, int i)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            if (i == 0)
            {
                if (request.Cookies.Get(name) != null)
                    return HttpUtility.UrlDecode(request.Cookies[name].Value, stre);
                else
                    return null;
            }
            else
            {
                if (request.Cookies.Get(name) != null)
                    return HttpUtility.UrlDecode(request.Cookies[name].Value, stre);
                else
                    return null;
            }
        }

        /// <summary>
        /// 根据Cookie名称和项获取Cookie
        /// </summary>
        /// <param name="CookieName">Cookie名称</param>
        /// <param name="name">Cookie项</param>
        /// <param name="encoding">对Cookie进行UTF编码</param>
        /// <returns></returns>
        public static string GetCookie(string CookieName, string name, string encoding)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            HttpRequest request = HttpContext.Current.Request;
            HttpCookie cookie = request.Cookies.Get(CookieName);
            if (cookie != null)
            {
                if (!string.IsNullOrEmpty(name) && cookie.HasKeys)
                    return HttpUtility.UrlDecode(cookie.Values[name], stre);
                else
                    return HttpUtility.UrlDecode(cookie.Value, stre);
            }
            return null;
        }

        /// <summary>
        /// 获得Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns>获取加前缀后的值</returns>
        public static HttpCookie GetCookies(string cookieName)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request != null)
                return request.Cookies[cookieName];
            return null;
        }

        /// <summary>
        /// 获得Cookie
        /// </summary>
        /// <param name="cookieName">缓存名称</param>
        /// <param name="i">0为不需要前缀，1为需要前缀</param>
        /// <returns></returns>
        public static HttpCookie GetCookies(string cookieName, int i)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request != null)
            {
                if (i == 0) return request.Cookies[cookieName];
                else return request.Cookies[cookieName];
            }
            return null;
        }

        #endregion

        #region 设置Cookie
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        public static void SetCookie(string name, string value)
        {
            SetCookie(name, value, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="i">0为不需要前缀，1为需要前缀</param>
        public static void SetCookie(string name, string value, int i)
        {
            if (i == 0) SetCookie(name, value, encoding);
            else SetCookie(name, value, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        public static void SetCookie(string name, string value, string encoding)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpRequest request = HttpContext.Current.Request;
            SetCookie(name, value, response, request, 1, encoding);
        }

        /// <summary>
        /// 设置Cookie,加过期时间
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="hours">过期时间(小时)</param>
        public static void SetCookie(string name, string value, int hours, int i)
        {
            if (i == 0) SetCookie(name, value, hours, encoding);
            else SetCookie(name, value, hours, encoding);
        }

        /// <summary>
        /// 设置Cookie,加过期时间
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="hours">过期时间(小时)</param>
        public static void SetCookie(string name, string value, int hours, string encoding)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpRequest request = HttpContext.Current.Request;
            SetCookie(name, value, response, request, hours, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="response">HttpResponse</param>
        /// <param name="request">HttpRequest</param>
        public static void SetCookie(string name, string value, HttpResponse response, HttpRequest request)
        {
            SetCookie(name, value, response, request, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="response">HttpResponse</param>
        /// <param name="request">HttpRequest</param>
        public static void SetCookie(string name, string value, HttpResponse response, HttpRequest request, int i)
        {
            if (i == 0) SetCookie(name, value, response, request, encoding);
            else SetCookie(name, value, response, request, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="response">HttpResponse</param>
        /// <param name="request">HttpRequest</param>
        public static void SetCookie(string name, string value, HttpResponse response, HttpRequest request, string encoding)
        {
            SetCookie(name, value, response, request, 1, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="response">HttpResponse</param>
        /// <param name="request">HttpRequest</param>
        /// <param name="hours">过期时间(小时)</param>
        public static void SetCookie(string name, string value, HttpResponse response, HttpRequest request, int hours, string encoding)
        {
            HttpCookie MyCookie;
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            //Response
            if (response.Cookies.Get(name) != null)
                MyCookie = response.Cookies[name];
            else
                MyCookie = new HttpCookie(name);
            DateTime now = DateTime.Now;
            MyCookie.Value = HttpUtility.UrlEncode(value, stre);
            MyCookie.Expires = now.AddHours(hours);
            MyCookie.Path = request.ApplicationPath;
            if (response.Cookies.Get(name) == null)
                response.Cookies.Add(MyCookie);

            //Request			
            if (request.Cookies.Get(name) != null)
                MyCookie = request.Cookies[name];
            else
                MyCookie = new HttpCookie(name);
            MyCookie.Value = HttpUtility.UrlEncode(value, stre);
            MyCookie.Expires = now.AddHours(hours);
            MyCookie.Path = request.ApplicationPath;
        }



        /// <summary>
        /// 设置Cookie子键的值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie1(string cookieName, string key, string value)
        {
            SetCookie1(cookieName, key, value, null, encoding);
        }

        /// <summary>
        /// 设置Cookie子键的值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie1(string cookieName, string key, string value, int i)
        {
            if (i == 0) SetCookie1(cookieName, key, value, null, encoding);
            else SetCookie1(cookieName, key, value, null, encoding);
        }

        /// <summary>
        /// 设置Cookie值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie1(string key, string value)
        {
            SetCookie1(key, null, value, null, encoding);
        }

        /// <summary>
        /// 设置Cookie值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie1(string key, string value, int i)
        {
            if (i == 0) SetCookie1(key, null, value, null, encoding);
            else SetCookie1(key, null, value, null, encoding);
        }

        /// <summary>
        /// 设置Cookie值和过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void SetCookie1(string key, string value, DateTime expires)
        {
            SetCookie1(key, null, value, expires, encoding);
        }

        /// <summary>
        /// 设置Cookie值和过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void SetCookie1(string key, string value, DateTime expires, int i)
        {
            if (i == 0) SetCookie1(key, null, value, expires, encoding);
            else SetCookie1(key, null, value, expires, encoding);
        }

        /// <summary>
        /// 设置Cookie过期时间
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="expires"></param>
        public static void SetCookie1(string cookieName, DateTime expires)
        {
            SetCookie1(cookieName, null, null, expires, encoding);
        }

        /// <summary>
        /// 设置Cookie过期时间
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="expires"></param>
        public static void SetCookie1(string cookieName, DateTime expires, int i)
        {
            if (i == 0) SetCookie1(cookieName, null, null, expires, encoding);
            else SetCookie1(cookieName, null, null, expires, encoding);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="hours"></param>
        /// <param name="encoding"></param>
        public static void SetCookie1(string cookieName, string key, string value, DateTime? expires, string encoding)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpRequest request = HttpContext.Current.Request;
            HttpCookie MyCookie;
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            //Response
            if (response.Cookies.Get(cookieName) != null)
                MyCookie = response.Cookies[cookieName];
            else
                MyCookie = new HttpCookie(cookieName);

            if (MyCookie != null)
            {
                if (!string.IsNullOrEmpty(key) && MyCookie.HasKeys)
                    MyCookie.Values.Set(key, HttpUtility.UrlEncode(value, stre));
                else
                    if (!string.IsNullOrEmpty(value))
                        MyCookie.Value = HttpUtility.UrlEncode(value, stre);
                if (expires != null)
                    MyCookie.Expires = expires.Value;
                MyCookie.Path = request.ApplicationPath;
                response.SetCookie(MyCookie);
            }
        }

        #endregion

        #region "删除Cookie"

        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        public static void RemoveCookie(string name)
        {
            //HttpResponse response = HttpContext.Current.Response;
            //HttpRequest request = HttpContext.Current.Request;
            //RemoveCookie(name, request, response);
            RemoveCookie(name, null);
        }

        /// <summary>
        /// 删除Cookie的子键
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        public static void RemoveCookie(string cookieName, string key)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                HttpCookie cookie = response.Cookies[cookieName];
                if (cookie != null)
                {
                    if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                        cookie.Values.Remove(key);
                    else
                        response.Cookies.Remove(cookieName);
                }
            }
        }

        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="Request">HttpRequest</param>
        /// <param name="Response">HttpResponse</param>
        public static void RemoveCookie(string name, HttpRequest Request, HttpResponse Response)
        {
            if (Request.Cookies.Get(name) == null) return;
            Request.Cookies.Remove(name);
            if (Response.Cookies.Get(name) == null) return;
            Response.Cookies.Remove(name);
        }
        #endregion

        #region 添加Cookie

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddCookie(string key, string value, string Domain)
        {
            AddCookie(key, value, Domain, encoding, 0);
        }

        public static void AddCookie(string key, string value, string Domain, string encoding, int num)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            AddCookie(new HttpCookie(key, HttpUtility.UrlEncode(value, stre)), Domain);
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void AddCookie(string key, string value, DateTime expires, string Domain)
        {
            AddCookie(key, value, expires, Domain, encoding);
        }

        public static void AddCookie(string key, string value, DateTime expires, string Domain, string encoding)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            HttpCookie cookie = new HttpCookie(key, HttpUtility.UrlEncode(value, stre));
            cookie.Expires = expires;
            AddCookie(cookie, Domain);
        }

        /// <summary>
        /// 添加为Cookie.Values集合
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddCookie(string cookieName, string key, string value, string domain)
        {
            AddCookie(cookieName, key, value, domain, encoding);
        }

        public static void AddCookie(string cookieName, string key, string value, string domain, string encoding)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Values.Add(key, HttpUtility.UrlEncode(value, stre));
            AddCookie(cookie, domain);
        }

        /// <summary>
        /// 添加为Cookie集合
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        /// <param name="expires">过期时间</param>
        public static void AddCookie(string cookieName, DateTime expires, string domain)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Expires = expires;
            AddCookie(cookie, domain);
        }

        /// <summary>
        /// 添加为Cookie.Values集合
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void AddCookie(string cookieName, string key, string value, DateTime expires, string domain)
        {
            AddCookie(cookieName, key, value, expires, domain, encoding);
        }

        public static void AddCookie(string cookieName, string key, string value, DateTime expires, string domain, string encoding)
        {
            encoding = (encoding.ToLower() == "gb2312" ? "gb2312" : "utf-8");
            Encoding stre = Encoding.GetEncoding(encoding);
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Expires = expires;
            cookie.Values.Add(key, HttpUtility.UrlEncode(value, stre));
            AddCookie(cookie, domain);
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie"></param>
        public static void AddCookie(HttpCookie cookie, string domain)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                //指定客户端脚本是否可以访问[默认为false]
                cookie.HttpOnly = true;
                //指定统一的Path，比便能通存通取
                cookie.Path = "/";
                //设置跨域,这样在其它二级域名下就都可以访问到了
                cookie.Domain = domain;
                response.AppendCookie(cookie);
            }
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie"></param>
        public static void AddCookie(HttpCookie cookie)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                //指定客户端脚本是否可以访问[默认为false]
                cookie.HttpOnly = true;
                //指定统一的Path，比便能通存通取
                cookie.Path = "/";
                //设置跨域,这样在其它二级域名下就都可以访问到了
                //cookie.Domain = domain;
                response.AppendCookie(cookie);
            }
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = strValue;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string key, string strValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie[key] = strValue;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="expires">过期时间(分钟)</param>
        public static void WriteCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        #endregion

        #endregion

    }
}
